// pages/myBankCard/myBankCardAdd.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    seleted: "",
    name:"",
    bankNane:"",
    bankNo:"",
    bankNoTo:"",
    Iphone:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    //获取手机高度
    that.setData({
      navT: app.globalData.navTop,
      navLH: app.globalData.navLineHeight,
      navH: app.globalData.navHeight,
      modelmes: app.globalData.modelmes
    })
    
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.request({
      url: app.globalData.url + "getUserBankcardList",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      data: {
        userId: wx.getStorageSync("userId"),
        token: wx.getStorageSync("token")
      },
      method: "POST",
      success: function (res) {
        var data = res.data;
        if (data.result == 1) {
          for (var i = 0; i < data.data.length;i++){
            data.data[i].cardNoFor = data.data[i].cardNo.substring(data.data[i].cardNo.length - 4);
          }
          that.setData({
            bankcard: data.data
          })
        }
        if (res.data.result == 9) {
          wx.reLaunch({
            url: '/pages/zcuslist/index',
          })
          return false;
        }
        if (res.data.result == 0) {
          wx.showToast({
            title: res.data.errmsg,
            icon: "none"
          })
          return false;
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  navBack: function () {
    var that = this;
    if (that.data.bankcard.length > 0) {
      for (var i = 0; i < that.data.bankcard.length; i++) {
        if (that.data.bankcard[i].bankcardid == that.data.bankcardid) {

        } else {
          let pages = getCurrentPages();
          let prevPage = pages[pages.length - 2];
          prevPage.setData({
            bankname: '',
            cardnofor: '',
            bankcardid: 0,
          })
        }

      }
    } else {
      let pages = getCurrentPages();
      let prevPage = pages[pages.length - 2];
      prevPage.setData({
        bankname: '',
        cardnofor: '',
        bankcardid: 0,
      })
    }
    
    wx.navigateBack({

    })
  },
  addBankCard:function(){
    wx.navigateTo({
      url: '/pages/bankCard/bankCard',
    })
  },
  selected:function(e){
    
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];
    prevPage.setData({
      bankname:e.currentTarget.dataset.bankname,
      cardnofor: e.currentTarget.dataset.cardnofor,
      bankcardid: e.currentTarget.dataset.bankcardid,
    })
    wx.navigateBack({
      
    })
  },
  del:function(e){
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定要删除吗？',
      success: function (sm) {
        if (sm.confirm) {
          wx.request({
            url: app.globalData.url + "deleteCardType",
            header: {
              "content-type": "application/x-www-form-urlencoded"
            },
            data: {
              userId: wx.getStorageSync("userId"),
              token: wx.getStorageSync("token"),
              bankcardId: e.currentTarget.dataset.bankcardid,
            },
            method: "POST",
            success: function (res) {
              var data = res.data;
              if (data.result == 1) {
                that.onShow();
              }
              if (res.data.result == 9) {
                wx.reLaunch({
                  url: '/pages/zcuslist/index',
                })
                return false;
              }
              if (res.data.result == 0) {
                wx.showToast({
                  title: res.data.errmsg,
                  icon: "none"
                })
                return false;
              }
            }
          });
        } else if (sm.cancel) {
        }
      }
    })
    
  }
})